% Computes IRF through "simulations" 
%(Note: Not clear if using companion matrix is more efficent)
%
% Syntax:
% imp=irfsim(b,smat,shock,p,nimp)
% 
% where the imputs are:     
%   b = Xt vector in the observation equation
%   smat = Cholesky factorization matrix
%   shock = vector with the shocks
%   p = number of lags
%   nimp = number of periods in the IRF
%
% and the outputs are:
%   imp = matrix with IRP 
%
%
%
%Jean Boivin, Marc Giannoni, Ilian Mihov
%11/08/01

function imp=irfsim(b,smat,shock,p,nimp);

b=b(2:end,:);
imp=(smat*shock)';     %first impulse
k=size(smat,1);

ly=[imp zeros(1,(p-1)*k)];      %values of rhs in VAR equation at t

%subsequent periods impulse 
for i=1:nimp
   
   impt	=	ly*b;
   ly 	=	[impt ly(:,1:(p-1)*k)];
   imp	=	cat(1,imp,impt);
   
end